package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pojo.*;
import service.RoomService;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/hotel")
public class RoomController {
    @Autowired
    @Qualifier("RoomServiceImpl")
    private RoomService roomService;

    @RequestMapping("/main")
    public String list(Model model) {
        List<Room> list = roomService.queryRoom();
        System.out.println(list);
        model.addAttribute("list", list);
        return "main";
    }

    /**
     * 增加房间
     */
    @RequestMapping("/toaddRoom")
    public String toaddRoom() {
        return "addRoom";
    }

    @RequestMapping("/addRoom")
    public String addRoom(@RequestParam("roomId") String roomId, @RequestParam("area") int area
            , @RequestParam("roomType") String roomType, @RequestParam("rentMoney") int rentMoney, @RequestParam("stated") String stated) {
        if (roomId != null && area != 0 && roomType != null && rentMoney != 0 && stated != null) {
            Room room = new Room();
            room.setRoomId(roomId);
            room.setRoomType(roomType);
            room.setArea(area);
            room.setRentMoney(rentMoney);
            room.setStated(stated);
            roomService.addRoom(room);
        }
        return "redirect:/hotel/main";
    }

    /**
     * 查看所有住户信息
     */
    @RequestMapping("/residentMessage")
    public String residentMessage(Model model) {
        List<Resident> list = roomService.queryResident();
        model.addAttribute("list", list);
        return "residentMessage";
    }

    /**
     * 预订房间
     */
    @RequestMapping("/toOrderRoom")
    public String toOrderRoom(Model model) {
        List<Room> list = roomService.canOrder();
        model.addAttribute("list", list);
        return "orderRoom";
    }

    @RequestMapping("/orderRoom")
    public String orderRoom(@RequestParam("room_id") String room_id, @RequestParam("card_id") String card_id, @RequestParam("name") String name,
                            @RequestParam("sex") String sex, @RequestParam("age") int age, @RequestParam("telephone") String telephone,
                            @RequestParam("subscription") int subscription) {
        Room room = new Room();
        room.setRoomId(room_id);
        roomService.updateState(room);
        Resident resident = new Resident();
        Date date = new Date();
        resident.setRoom_id(room_id);
        resident.setCard_id(card_id);
        resident.setName(name);
        resident.setAge(age);
        resident.setSex(sex);
        resident.setTelephone(telephone);
        resident.setSubscription(subscription);
        resident.setIntime(date);
        roomService.addResident(resident);
        return "redirect:/hotel/main";
    }

    /**
     * 修改房间信息
     */
    @RequestMapping("/toupdateRoom")
    public String toupdateRoom(Model model) {
        List<Room> list = roomService.queryRoom();
        model.addAttribute("list", list);
        return "updateRoom";
    }

    @RequestMapping("/updateRoom")
    public String updateRoom(@RequestParam("roomId") String roomId, @RequestParam("area") String area,
                             @RequestParam("roomType") String roomType, @RequestParam("rentMoney") String rentMoney,
                             @RequestParam("stated") String stated) {
        Room room = new Room();
        if (roomId != null && !roomId.equals("")) {
            room.setRoomId(roomId);
            if (area != null && !area.equals("")) {
                System.out.println("你好*************");
                int area1 = Integer.parseInt(area);
                if (area1 != 0) {
                    room.setArea(area1);
                }
            }
            if (roomType != null && !roomType.equals("")) {
                room.setRoomType(roomType);
            }
            if (rentMoney != null && !rentMoney.equals("")) {
                int rentMoney1 = Integer.parseInt(rentMoney);
                if (rentMoney1 != 0) {
                    room.setRentMoney(rentMoney1);
                }
            }
            if (stated != null && !stated.equals("")) {
                room.setStated(stated);
            }
            roomService.updateRoom(room);
            return "redirect:/hotel/main";
        } else {
            return "redirect:/hotel/error";
        }


    }

    /**
     * 修改信息报错提醒
     * @return
     */
    @RequestMapping("/error")
    public String error() {
        return "error";
    }

    /**
     * 增加住客信息
     */
    @RequestMapping("/toaddResident")
    public String toaddResident(Model model){
        List<Room> list=roomService.notOrder();
        model.addAttribute("list",list);
        return "addResident";
    }
    @RequestMapping("/addResident")
    public String addResident(@RequestParam("room_id") String room_id, @RequestParam("card_id") String card_id, @RequestParam("name") String name,
                              @RequestParam("sex") String sex, @RequestParam("age") int age, @RequestParam("telephone") String telephone,
                              @RequestParam("subscription") int subscription){
        Resident resident=new Resident();
        Date date=new Date();
        resident.setRoom_id(room_id);
        resident.setName(name);
        resident.setCard_id(card_id);
        resident.setAge(age);
        resident.setSubscription(subscription);
        resident.setTelephone(telephone);
        resident.setSex(sex);
        resident.setIntime(date);
        roomService.addResident(resident);
        return "redirect:/hotel/main";
    }
    /**
     * 修改住客信息
     */
    @RequestMapping("/toUpdateResident")
    public String toUpdateResident(Model model){
        List<Resident> list=roomService.queryResident();
        model.addAttribute("list",list);
        return "updateResident";
    }
    @RequestMapping("/updateResident")
    public String updateResident(@RequestParam("room_id") String room_id, @RequestParam("card_id") String card_id,
                                 @RequestParam("name") String name,@RequestParam("sex") String sex,
                                 @RequestParam("age") String age, @RequestParam("telephone") String telephone){
        Resident resident=new Resident();
        if(card_id!=null&&!card_id.equals("")){
            resident.setCard_id(card_id);
            if (room_id!=null&&!room_id.equals("")){
                resident.setRoom_id(room_id);
            }
            if(name!=null&&!name.equals("")){
                resident.setName(name);
            }
            if(sex!=null&&!sex.equals("")){
                resident.setSex(sex);
            }
            if(age!=null&&!age.equals("")){
                int age1=Integer.parseInt(age);
                resident.setAge(age1);
            }
            if (telephone!=null&&!telephone.equals("")){
                resident.setTelephone(telephone);
            }
            roomService.updateResident(resident);
            return "redirect:/hotel/main";
        }else{
            return "redirect:/hotel/error";
        }
    }
    /**
     * 退房操作
     */
    @RequestMapping("toCheckOutRoom")
    public String toCheckOutRoom(Model model){
        List<Room> list=roomService.notOrder();
        model.addAttribute("list",list);
        return "checkOutRoom";
    }
    @RequestMapping("checkOutRoom")
    public String checkOutRoom(@RequestParam("room_id")String room_id ,@RequestParam("name") String name,Model model){
        Resident resident=new Resident();
        Room room=new Room();
        room.setRoomId(room_id);
        resident.setRoom_id(room_id);
        resident.setName(name);
        List<Resident> list=roomService.roomResident(resident);
        Date date=new Date();
        int rentMoney=roomService.receiveRentMoney(room);
        int sum=roomService.receiveSubscription(resident);
        model.addAttribute("rentMoney",rentMoney);
        model.addAttribute("sum",sum);
        roomService.deleteResident(resident);       //将住客的信息从住客表中删除
        if(roomService.selectResident(resident)!=null){
            roomService.updateStateOpen(room);     //将房间的状态修改为可订状态
            for(int i=0;i<list.size();i++){
                Resident newResident=new Resident();
                newResident.setRoom_id(list.get(i).getRoom_id());
                newResident.setCard_id(list.get(i).getCard_id());
                newResident.setName(list.get(i).getName());
                newResident.setAge(list.get(i).getAge());
                newResident.setSex(list.get(i).getSex());
                newResident.setTelephone(list.get(i).getTelephone());
                newResident.setIntime(list.get(i).getIntime());
                newResident.setOuttime(date);
                roomService.addHistoryResident(newResident);  //将该住客的信息写入历史记录表中
            }
        }
        return "total";
    }
    @RequestMapping("/total")
    public String total(){
        return "total";
    }

    /**
     * 查看所有历史订单
     */
    @RequestMapping("/allHistoryResident")
    public String allHistoryResident(Model model){
        List<HistoryResident> list=roomService.allHistory();
        model.addAttribute("list",list);
        return "allHistoryResident";
    }

    /**
     * 查询历史订单中的房间预订次数
     */
    @RequestMapping("/roomCount")
    public String roomCount(Model model){
        List<RoomCount> list=roomService.selectCount();
        model.addAttribute("list",list);
        return "roomCount";
    }
    /**
     * 删除住房
     */
    @RequestMapping("/todeleteRoom")
    public String toDeleteRoom(){
        return "deleteRoom";
    }
    @RequestMapping("deleteRoom")
    public String deleteRoom(@RequestParam("room_id")String roomId){
        Room room=new Room();
        room.setRoomId(roomId);
        roomService.deleteRoom(room);
        return "redirect:/hotel/main";
    }
    /**
     * 按名字查看是否有该住客
     */
    @RequestMapping("/tofindResident")
    public String toFindResident(){
        return "findResident";
    }
    @RequestMapping("/canFindResident")
    public String canFindResident(){
        return "canFindResident";
    }
    @RequestMapping("/notFindResident")
    public String notFindResident(){
        return "notFindResident";
    }
    @RequestMapping("/findResident")
    public String findResident(@RequestParam("name") String name,@RequestParam("card_id") String card_id,Model model){
        Resident resident=new Resident();
        resident.setName(name);
        resident.setCard_id(card_id);
        String roomId=roomService.findRoomId(resident);
        if(roomService.findResident(resident)!=null&&roomId!=null){
            model.addAttribute("roomId",roomId);
            return "canFindResident";
        }else {
            return "notFindResident";
        }
    }
    /**
     * 活动开启
     */
    @RequestMapping("/happy")
    public String happyDay(){
        roomService.updateRentMoney();
        return "happy";
    }
    /**
     * 活动结束
     */
    @RequestMapping("/overHappy")
    public String overHappy(){
        roomService.restoreRentMoney();
        return "overHappy";
    }
    /**
     * 统计一年中的收益
     */
    @RequestMapping("/toIncome")
    public String toIncome(){
        return "income";
    }
    @RequestMapping("/income")
    public String income(@RequestParam("year") String year,Model model){
        Year y=new Year();
        y.setYear(year);
        if(roomService.totalMoney(y)!=null) {
            int money = roomService.totalMoney(y);
            model.addAttribute("money", money);
            if(roomService.findYear(y)!=null){ //如果表中没数据 就插入进去，如果有数据 就修改金钱
                Year yy = new Year();
                yy.setYear(year);
                yy.setMoney(money);
                roomService.updateIncomeMoney(yy);
            }else {
                Year yy = new Year();
                yy.setYear(year);
                yy.setMoney(money);
                roomService.addIncomeMoney(yy);//将收益情况写入收益表中
            }
            return "income1";
        }else{
            model.addAttribute("money", 0);
            Year a = new Year();
            a.setYear(year);
            a.setMoney(0);
            if(roomService.findYear(y)!=null) {
                roomService.updateIncomeMoney(a);
            }else {
                roomService.addIncomeMoney(a);
            }
            return "income1";
        }
    }
    /**
     * 退出登录
     */
    @RequestMapping("/quit")
    public String quit(){
        return "login";
    }



}
